<template>
  <el-dialog
    :title="!dataForm.departmentAdministratorId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="账号" prop="administratorAccount">
        <el-input v-model="dataForm.administratorAccount" placeholder="管理员账号"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="administratorPwd">
        <el-input v-model="dataForm.administratorPwd" type="password" placeholder="管理员密码" :class="{ 'is-required': !dataForm.departmentAdministratorId }"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="comfirmPassword">
        <el-input v-model="dataForm.comfirmPassword" type="password" placeholder="确认密码" :class="{ 'is-required': !dataForm.departmentAdministratorId }"></el-input>
      </el-form-item>
      <el-form-item label="昵称" prop="administratorNickname">
        <el-input v-model="dataForm.administratorNickname" placeholder="管理员昵称"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="administratorRemark">
        <el-input v-model="dataForm.administratorRemark" placeholder="管理员备注"></el-input>
      </el-form-item>
      <el-form-item label="联系电话" prop="administratorContactPhone">
        <el-input v-model="dataForm.administratorContactPhone" placeholder="管理员联系电话"></el-input>
      </el-form-item>
      <el-form-item label="微信" prop="administratorWechat">
        <el-input v-model="dataForm.administratorWechat" placeholder="管理员微信"></el-input>
      </el-form-item>
      <el-form-item label="科室" prop="departmentId">
        <template slot-scope="scope">
          <el-select v-model="dataForm.hospitalId" filterable v-bind:disabled="dataForm.isHospitalManager==1" placeholder="请选择医院" clearable  @change="hospitalChanged">
            <el-option
              v-for="item in hospitalList"
              :key="item.hospitalId"
              :label="item.hospitalName"
              :value="item.hospitalId">
            </el-option>
          </el-select>
          <el-select v-model="dataForm.departmentId" filterable placeholder="请选择科室" clearable>
            <el-option
              v-for="item in departmentList"
              :key="item.departmentId"
              :label="item.departmentName"
              :value="item.departmentId">
            </el-option>
          </el-select>
        </template>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    export default {
        data() {
            var validatePassword = (rule, value, callback) => {
                if (!this.dataForm.departmentAdministratorId && !/\S/.test(value)) {
                    callback(new Error('密码不能为空'))
                } else {
                    callback()
                }
            }
            var validateComfirmPassword = (rule, value, callback) => {
                if (!this.dataForm.departmentAdministratorId && !/\S/.test(value)) {
                    callback(new Error('确认密码不能为空'))
                } else if (this.dataForm.administratorPwd !== value) {
                    callback(new Error('确认密码与密码输入不一致'))
                } else {
                    callback()
                }
            }
            var validateEmail = (rule, value, callback) => {
                if (!isEmail(value)) {
                    callback(new Error('邮箱格式错误'))
                } else {
                    callback()
                }
            }
            var validateMobile = (rule, value, callback) => {
                if (!isMobile(value)) {
                    callback(new Error('手机号格式错误'))
                } else {
                    callback()
                }
            }
            return {
                visible: false,
                departmentOptions: [],
                hospitalList:'',
                departmentList:'',
                dataForm: {
                    //科室
                    departmentAdministratorId: 0,
                    administratorAccount: '',
                    administratorPwd: '',
                    administratorNickname: '',
                    administratorRemark: '',
                    comfirmPassword: '',
                    administratorContactPhone: '',
                    administratorWechat: '',
                    departmentId: '',
                    hospitalId: this.$cookie.get('hospitalId')!="null"?parseInt(this.$cookie.get('hospitalId')):'',
                    isHospitalManager: this.$cookie.get('isHospitalManager')
                },
                dataRule: {
                    administratorAccount: [
                        {required: true, message: '管理员账号不能为空', trigger: 'blur'}
                    ],
                    administratorPwd: [
                        { validator: validatePassword,  message: '管理员密码不能为空', trigger: 'blur'}
                    ],
                    comfirmPassword: [
                        { validator: validateComfirmPassword, trigger: 'blur' }
                    ],
                    administratorNickname: [
                        {required: true, message: '管理员昵称不能为空', trigger: 'blur'}
                    ],
                    // administratorContactPhone: [
                    //     {validator: validateMobile, trigger: 'blur'}
                    // ],
                    departmentId: [
                        {required: true, message: '科室不能为空', trigger: 'blur'}
                    ]
                }
            }
        },
        created() {
            //加载医院
            this.$http({
                url: this.$http.adornUrl(`/bs/hospital/getList`),
                method: 'get',
                params:this.$http.adornParams()
            }).then(({data}) => {
                if (data && data.code === 0) {
                    this.hospitalList = data.res;
                }
            })
        },

        methods: {
            hospitalChanged(hospitalId){
                if(hospitalId != ''){
                    //加载科室
                    this.$http({
                        url: this.$http.adornUrl(`/bs/department/getListByHospitalId`),
                        method: 'get',
                        params:this.$http.adornParams({
                            hospitalId:hospitalId
                        })
                    }).then(({data}) => {
                        if (data && data.code === 0) {
                            this.dataForm.departmentId = '';
                            this.departmentList = data.res;
                        }
                    })
                }else {
                    this.departmentList = [];
                    this.dataForm.departmentId = '';
                }
            },
            init(id) {
                this.dataForm.departmentAdministratorId = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (this.dataForm.departmentAdministratorId) {
                        this.$http({
                            url: this.$http.adornUrl(`/bs/departmentAdministrator/info/${this.dataForm.departmentAdministratorId}`),
                            method: 'get',
                            params: this.$http.adornParams()
                        }).then(({data}) => {
                            if (data && data.code === 0) {
                                this.dataForm.administratorAccount = data.bsDepartmentAdministrator.administratorAccount
                                this.dataForm.administratorPwd = data.bsDepartmentAdministrator.administratorPwd
                                this.dataForm.administratorNickname = data.bsDepartmentAdministrator.administratorNickname
                                this.dataForm.administratorRemark = data.bsDepartmentAdministrator.administratorRemark
                                this.dataForm.administratorContactPhone = data.bsDepartmentAdministrator.administratorContactPhone
                                this.dataForm.administratorWechat = data.bsDepartmentAdministrator.administratorWechat
                                this.dataForm.departmentId = data.bsDepartmentAdministrator.departmentId
                                this.dataForm.hospitalId = data.bsDepartmentAdministrator.hospitalId
                            }
                        })
                    }
                    //加载科室
                    this.hospitalChanged(this.dataForm.hospitalId);
                })
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.$http({
                            url: this.$http.adornUrl(`/bs/departmentAdministrator/${!this.dataForm.departmentAdministratorId ? 'save' : 'update'}`),
                            method: 'post',
                            data: this.$http.adornData({
                                'departmentAdministratorId': this.dataForm.departmentAdministratorId || undefined,
                                'administratorAccount': this.dataForm.administratorAccount,
                                'administratorPwd': this.dataForm.administratorPwd,
                                'administratorNickname': this.dataForm.administratorNickname,
                                'administratorRemark': this.dataForm.administratorRemark,
                                'administratorContactPhone': this.dataForm.administratorContactPhone,
                                'administratorWechat': this.dataForm.administratorWechat,
                                'departmentId': this.dataForm.departmentId
                            })
                        }).then(({data}) => {
                            if (data && data.code === 0) {
                                this.$message({
                                    message: '操作成功',
                                    type: 'success',
                                    duration: 1500,
                                    onClose: () => {
                                        this.visible = false
                                        this.$emit('refreshDataList')
                                    }
                                })
                            } else {
                                this.$message.error(data.msg)
                            }
                        })
                    }
                })
            }
        }
    }
</script>
